package com.huluobo.a2207douyindemo.home;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.base.bean.BannerBeanResult;
import com.huluobo.base.bean.VideoBeanResult;
import com.huluobo.base.mvvm.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by LC on 2024/9/28.
 */
public class HomeRepo extends BaseRepo {
    public void getHomeVideos(int currentPage, int pageSize, MutableLiveData<VideoBeanResult> success,
                              MutableLiveData<String> failed) {
        apiService.getVideos(currentPage, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoBeanResult videoBeanResult) {
                        success.setValue(videoBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getHomeBanner(MutableLiveData<BannerBeanResult> success, MutableLiveData<String> failed) {
        apiService.getBanner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BannerBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BannerBeanResult bannerBeanResult) {
                        success.setValue(bannerBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void findVideoByName(String keyword, MutableLiveData<VideoBeanResult> success,
                                MutableLiveData<String> failed) {
        apiService.findVideoByName(keyword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoBeanResult videoBeanResult) {
                        success.setValue(videoBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
